﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;


namespace Topro.Admin.Controllers
{
    using System.Linq;
    using Topro.Application.Interface;
    using Topro.Entity;
    using Topro.Entity.Enum;
    using Topro.Entity.Query;
    using Topro.Entity.Request.InventoryItem;
    using Topro.Entity.Request.StockIn;
    using Topro.Util;

    /// <summary>
    /// 出入库控制器
    /// </summary>
    public class StockInController : BaseController
    {
        #region 注入
        /// <summary>
        /// 
        /// </summary>
        private readonly IUId _uid;

        /// <summary>
        /// 日志接口
        /// </summary>
        private readonly LogService _logService;

        /// <summary>
        /// 入库接口
        /// </summary>
        private readonly StockInService _stockInService;

        /// <summary>
        /// 库存接口
        /// </summary>
        private readonly InventoryService _inventoryService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logService">日志接口</param>
        /// <param name="stockInService">入库接口</param>
        public StockInController(LogService logService, IUId uid, StockInService stockInService, InventoryService inventoryService)
        {
            _logService = logService;
            _uid = uid;
            _stockInService = stockInService;
            _inventoryService = inventoryService;
        }
        #endregion

        #region 入库
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public new IActionResult Index()
        {
            ViewBag.StockInStatus = (Dictionary<int, string>)EnumUtil.ToDictionary(typeof(StockInStatus));

            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>DataGrid<StockInQueryResponse></returns>
        public JsonResult StockInQuery(StockInQuery query)
        {
            query.Status = new List<int>()
            {
                (int)StockInStatus.待入库,
                (int)StockInStatus.入库中,
                (int)StockInStatus.入库完成,
            };

            var list = _stockInService.Pager(query);

            return Json(list);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>DataGrid<StockInQueryResponse></returns>
        public JsonResult InventoryQuery(InventoryQuery query)
        {

            query.OrderType = (int)OrderType.入库;
            query.Status = (int)InventoryStatus.待处理;

            var list = _inventoryService.Pager(query);

            return Json(list);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public IActionResult StockInAddOrEdit()
        {
            ViewBag.NextId = _uid.NextId();

            return View("StockInAddOrEdit");
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public JsonResult Save(StockInSaveRequest stockIn, List<InventoryItemSaveRequest> inventoryItem)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                //验证单据编号
                if (string.IsNullOrEmpty(stockIn.OrderId))
                {
                    throw new Exception("订单号不能为空.");
                }
                //方位
                //var fixs = new List<int>()
                //{
                //    (int)Fix.内,
                //    (int)Fix.外
                //};

                //if (!fixs.Any(c => c == stockIn.Fix))
                //{
                //    throw new Exception("请选择库位方位.");
                //}

                //容器类型
                //var containerType = new List<int>()
                //{
                //    (int)ContainerType.小容器,
                //    (int)ContainerType.大容器
                //};

                //if (!containerType.Any(c=>c == stockIn.ContainerType))
                //{
                //    throw new Exception("请选择容器类型.");
                //}
                //创建人
                stockIn.CreatedBy = UserId;
                response = _stockInService.Save(stockIn, inventoryItem);

            }
            catch(Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }

        /// <summary>
        /// 入库审核
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult StockInApproval(string id)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                //response = _stockInOutService.StockInApproval(id);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
        #endregion
    }
}
